package com.book.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.book.dao.ApplyDao;
import com.book.dao.PostDao;
import com.book.dto.PostDto;
import com.book.dto.PostPage;
import com.book.entity.Apply;
import com.book.entity.Post;
import com.book.service.PostService;
import com.book.utils.ResponseResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

//错误码从9001开始
@Service
public class PostServiceImpl implements PostService {

    @Autowired
    private PostDao postDao;
    @Autowired
    private ApplyDao applyDao;
    @Override
    public ResponseResult getList(PostPage postPage) {
        try {
            PageHelper.startPage(postPage.getCurrentPage(),postPage.getPageSize());
            List<PostDto> list = postDao.getList(postPage.getMessage());
            PageInfo<PostDto> pageInfo = new PageInfo<>(list);
//            System.out.println(pageInfo);
            return new ResponseResult(200,"获取数据成功",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(9001,"获取数据失败");
        }
    }

    @Override
    public ResponseResult publishPost(String userId,Post post) {
        //无论是添加还是更新，时间都设置为添加或更新后的那个事件
        post.setPostingTime(new Date());
        try {
            if(post.getId() == 0){
                //说明是添加帖子的操作
                post.setUserId(userId);
                System.out.println(post);
                postDao.insert(post);
            }else {
                //如果id不为null，说明是更新，因为数据库已经存在这个id说明本身有这个帖子
                postDao.updateById(post);
            }
            return new ResponseResult(200,"发表成功");
        }catch(Exception e){
            e.printStackTrace();
            return new ResponseResult(9002,"发表失败");
        }
    }

    @Override
    public ResponseResult getOne(int id) {
        try {
            return new ResponseResult(200,"获取帖子详情成功",postDao.getOne(id));
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(9003,"获取失败");
        }
    }

    @Override
    public ResponseResult getPost(int id) {
        try {
            return new ResponseResult(200,"获取成功",postDao.selectById(id));
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(9004,"获取帖子详情失败");
        }
    }

    @Override
    public ResponseResult getPosts(PostPage postPage) {
        try {
            QueryWrapper<Post> qw = new QueryWrapper<>();
            qw.eq("user_id",postPage.getUserId());
            qw.like("title",postPage.getMessage());
            qw.orderByDesc("posting_time");
            PageHelper.startPage(postPage.getCurrentPage(),postPage.getPageSize());
            List<Post> posts = postDao.selectList(qw);
            PageInfo<Post> postPageInfo = new PageInfo<>(posts);

            if(postPage.getCurrentPage()>postPageInfo.getPages()){
                PageHelper.startPage(postPageInfo.getPages(),postPage.getPageSize());
                posts = postDao.selectList(qw);
                postPageInfo = new PageInfo<>(posts);
            }
            return new ResponseResult(200,"获取自己所有帖子成功",postPageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(9005,"获取自己所有帖子失败");
        }
    }

    @Override
    public ResponseResult deletePost(int id) {
        try {
            postDao.deleteById(id);//删除帖子
            QueryWrapper<Apply> qw = new QueryWrapper<>();
            qw.eq("post_id",id);
            applyDao.delete(qw);//删除帖子下的所有回复
            return new ResponseResult(200,"删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(9006,"删除失败");
        }
    }

    @Override
    public ResponseResult getAllPosts(PostPage postPage) {
        try {
            QueryWrapper<Post> qw = new QueryWrapper<>();
            qw.like("title",postPage.getMessage()).like("content",postPage.getMessage())
                    .like("user_id",postPage.getUserId());
            if(postPage.getPostId()!=0){
                qw.eq("id",postPage.getPostId());
            }
            qw.orderByDesc("posting_time");
            PageHelper.startPage(postPage.getCurrentPage(),postPage.getPageSize());
            List<Post> list = postDao.selectList(qw);
            PageInfo<Post> pageInfo = new PageInfo<>(list);
//            System.out.println(pageInfo);
            return new ResponseResult(200,"获取数据成功",pageInfo);
        }catch (Exception e){
            e.printStackTrace();
            return new ResponseResult(9007,"获取数据失败");
        }
    }
}
